package development.game;

import java.util.Vector;

import state.GameAlert;
import state.GameConstants;
import state.GameForm;
import state.GameGlobal;
import state.GameImage;
import state.Graphics;
import state.Key;
import state.TimerListener;
import state.component.TextField;
import state.font.FontManager;
import state.font.ImageText;
import state.socket.DataPackage;
import state.socket.DataReceiveListener;
import state.util.GameColor;
import state.util.GameStringUtils;
import state.util.Hashtable;
import state.util.ImageUtil;
import state.util.NumberUtil;
import state.util.TwoDimensionInt;
import state.util.array.StringArray;
import development.Constants;
import development.DrawUtils;
import development.Global;
import development.bean.Game;
import development.bean.User;
import development.group.FrmGroupListTableFreeUserChat;
import development.group.FrmGroupTableFreeUserFriend;
import development.socket.SocketClientUtil;

public class FrmBaCay extends GameForm implements DataReceiveListener, TimerListener {
  private final int PREPARE_GAME_RESPONSE = 2001;
  private final int BET_REQUEST = 3001;
  private final int BET_RESPONSE = 4001;
  private final int COMMIT_REQUEST = 5001;
  private final int COMMIT_RESPONSE = 6001;
  private final int USER_LEAVE_TABLE_REQUEST = 7001;
  private final int USER_LEAVE_TABLE_RESPONSE = 8001;
  private static final int USER_VIEW_JOIN_HEADER = 9001;
  private static final int USER_VIEW_IN_BETTING_TIME_RESPONSE = 9002;

  private static final int VIEW_USER_STATUS = 4;

  private GameImage imgBaiUp;

  private ImageText text8;
  private ImageText text13;
  private GameImage dollarImage;
  private GameImage notifyImage;
  private GameImage notifyEndGameImage;
  private GameImage notifyImage2;
  private GameImage notifyEndGameImage2;
  private Vector userInfoList;
  private UserInfo currentUserInfo;
  private boolean isFinish;
  private boolean isBet = false;
  private Hashtable cardImageList;
  private GameImage defaultAvatar;
  private GameImage clockImage;
  private GameImage tableImage;

  private boolean isShowBetBoard = false;
  private int myBetMoney;
  private TextField txtBet;

  // Thời gian chờ là 20 giây
  private int waitingBetCount;
  private int waitingCommitCount;
  private int waitingContinueGame;

  // Thời gian show thông báo là 20 giây
  private final int MAX_SHOW_NOTIFY_COUNT = 20;

  private static GameForm singleton;

  public static GameForm instance() {
    if (singleton == null) {
      singleton = new FrmBaCay();
    }
    return singleton;
  }

  public void init(Hashtable parameters) {
    dollarImage = ImageUtil.getImage(Constants.BA_CAY_IMAGE_CATEGORY, "dollar.png");
    notifyImage = ImageUtil.getImage(Constants.BA_CAY_IMAGE_CATEGORY, "notify1.png");
    notifyImage2 = ImageUtil.rotate(notifyImage, 180);
    notifyEndGameImage = ImageUtil.getImage(Constants.BA_CAY_IMAGE_CATEGORY, "notify2.png");
    notifyEndGameImage2 = ImageUtil.rotate(notifyEndGameImage, 180);
    text8 = FontManager.getFont(FontManager.FONT_SIZE_8);
    text13 = FontManager.getFont(FontManager.FONT_SIZE_13);
    defaultAvatar = Global.getDefaultAvatar();
    tableImage = ImageUtil.getImage(Constants.CARD_IMAGE_CATEGORY, "bg_Danh-bai.png");

    clockImage = ImageUtil.getImage(Constants.BA_CAY_IMAGE_CATEGORY, "clock.png");
    cardImageList = new Hashtable();
    imgBaiUp = ImageUtil.getImage(Constants.CARD_IMAGE_CATEGORY, "Bai_MSau.png");

    txtBet = new TextField(_manager, TextField.CONSTRAINT_NUMERIC, null);
    txtBet.setSize(GameConstants.SCREEN_WIDTH / 2 + 7, text8.getHeight());
    txtBet.setAnchor(GameConstants.BOTTOM_LEFT_ANCHOR);
    txtBet.setPosition(text8.stringWidth("Đặt cược:" + 5), GameConstants.SCREEN_HEIGHT - DrawUtils.getBottomBarHeight() - 15);
    txtBet.setText(String.valueOf(Global.currentTable.getBid()));
    txtBet.setVisible(false);

    // Thời gian chờ là 20 giây
    waitingBetCount = 200;
    waitingCommitCount = 200;
    waitingContinueGame = 150;

    // GameGlobal.setTimerDelay(1000);
    setTimerListener(this);
    myBetMoney = Global.currentTable.getBid();
    DrawUtils.setBottomBar(_manager, "Thoát", "", "");
    _manager.enableTouchPad();
  }

  public void alertEventPerform(int alertType, int eventType, int alertId) {
    if (eventType == GameAlert.YES_BUTTON && alertId == 97) {
      if (!isFinish && userInfoList.size() > 1) {
        Global.gameActionClient.write(Game.createGamePackage(USER_LEAVE_TABLE_REQUEST));
      } else {
        Global.nextState(FrmGroupListTableFreeUserChat.instance(), null);
      }
    }
  }

  // Vẽ khi có sự kiện, Vẽ quân bài, avatar
  public void draw(Graphics g) {
    DrawUtils.drawBackground(g);
    g.drawImage(tableImage, Constants.SCREEN_WIDTH / 2, (Constants.SCREEN_HEIGHT - 25) / 2, Constants.CENTER_ANCHOR);
    if (!isBet) {
      drawPrepare(g);
      // Thời gian cược
      if (currentUserInfo != null) {
        text13.drawString2(g, "Thời gian cược", GameColor.WHITE_CODE, GameConstants.SCREEN_WIDTH / 2 - 50, 15, GameConstants.TOP_LEFT_ANCHOR);
        g.drawImage(clockImage, GameConstants.SCREEN_WIDTH / 2 - 20, 37, GameConstants.TOP_LEFT_ANCHOR);
        text13.drawString2(g, String.valueOf(waitingBetCount / 10), GameColor.WHITE_CODE, GameConstants.SCREEN_WIDTH / 2 + 2, 37, GameConstants.TOP_LEFT_ANCHOR);
        if (isShowBetBoard && currentUserInfo.master == 0) {
          drawBet(g);
        }
      }
    } else {
      if (userInfoList != null && userInfoList.size() != 0) {
        if (!isFinish) {
          g.drawImage(clockImage, GameConstants.SCREEN_WIDTH / 2 - 20, 37, GameConstants.TOP_LEFT_ANCHOR);
          text13.drawString2(g, String.valueOf(waitingCommitCount / 10), GameColor.WHITE_CODE, GameConstants.SCREEN_WIDTH / 2 + 2, 37, GameConstants.TOP_LEFT_ANCHOR);
        }
        drawChiaBai(g);
      }
      drawLatBai(currentUserInfo, g);
      drawHaBai(g);
    }
    if (isFinish) {
      drawKetQua(g);
    }
    drawMenu(currentUserInfo, g);

    Global.drawChat(g);
  }

  private void drawBet(Graphics g) {
    g.setColor(GameColor.WHITE_CODE);
    g.fillRect(0, GameConstants.SCREEN_HEIGHT - DrawUtils.getBottomBarHeight() - 40, GameConstants.SCREEN_WIDTH, GameConstants.SCREEN_HEIGHT - DrawUtils.getBottomBarHeight() - 40);
    g.drawRect(0, GameConstants.SCREEN_HEIGHT - DrawUtils.getBottomBarHeight() - 40, GameConstants.SCREEN_WIDTH, GameConstants.SCREEN_HEIGHT - DrawUtils.getBottomBarHeight() - 40);
    text8.drawString2(g, "Đặt cược:", GameColor.BLACK_CODE, 5, GameConstants.SCREEN_HEIGHT - DrawUtils.getBottomBarHeight() - 15, GameConstants.BOTTOM_LEFT_ANCHOR);
    txtBet.draw(g);
  }

  private void drawKetQua(Graphics g) {
    int DX = 0;
    int DY = 0;
    if (Constants.IS_240x320_SCREEN) {
      DX = 28;
      DY = GameConstants.SCREEN_HEIGHT / 2 - 70;
    } else {
      DX = 75;
      DY = GameConstants.SCREEN_HEIGHT / 2 - 50;
    }
    DrawUtils.drawHozPopup(g, 183, DX, DY);
    for (int i = 0; i < userInfoList.size(); i++) {
      UserInfo usrInfo = (UserInfo) userInfoList.elementAt(i);
      if (usrInfo.status == VIEW_USER_STATUS) {
        continue;
      }
      if (usrInfo.master == 1) {
        text8.drawString2(g, usrInfo.user, GameColor.WHITE_CODE, text8.stringWidth("Thắng: ") + DX, DY, GameConstants.TOP_LEFT_ANCHOR);
        text8.drawString2(g, usrInfo.point, GameColor.WHITE_CODE, text8.stringWidth("Thắng: ") + DX + text8.stringWidth(usrInfo.user) + 15, DY, GameConstants.TOP_LEFT_ANCHOR);
        if (usrInfo.bet >= 0) {
          text8.drawString2(g, "+ " + String.valueOf(usrInfo.bet) + " gold", GameColor.WHITE_CODE, text8.stringWidth("Chương:") + DX + text8.stringWidth(usrInfo.user) + 15 + text8.stringWidth("10 nước")
              + 10, DY, GameConstants.TOP_LEFT_ANCHOR);
        } else {
          text8.drawString2(g, String.valueOf(usrInfo.bet) + " gold", GameColor.WHITE_CODE, text8.stringWidth("Chương:") + DX + text8.stringWidth(usrInfo.user) + 15 + text8.stringWidth("10 nước") + 10,
              DY, GameConstants.TOP_LEFT_ANCHOR);
        }
        DY = DY + 15;
      } else {
        if (usrInfo.win == 1) {
          text8.drawString2(g, "Thắng: ", GameColor.WHITE_CODE, DX, DY, GameConstants.TOP_LEFT_ANCHOR);
          text8.drawString2(g, usrInfo.user, GameColor.WHITE_CODE, text8.stringWidth("Thắng: ") + DX, DY, GameConstants.TOP_LEFT_ANCHOR);
          text8.drawString2(g, usrInfo.point, GameColor.WHITE_CODE, text8.stringWidth("Thắng: ") + DX + text8.stringWidth(usrInfo.user) + 15, DY, GameConstants.TOP_LEFT_ANCHOR);
          text8.drawString2(g, "+" + String.valueOf(usrInfo.bet) + " gold", GameColor.WHITE_CODE, text8.stringWidth("Thắng:") + DX + text8.stringWidth(usrInfo.user) + 15 + text8.stringWidth("10 nước")
              + 10, DY, GameConstants.TOP_LEFT_ANCHOR);
          DY = DY + 15;
        } else {
          text8.drawString2(g, "Thua: ", GameColor.WHITE_CODE, DX, DY, GameConstants.TOP_LEFT_ANCHOR);
          text8.drawString2(g, usrInfo.user, GameColor.WHITE_CODE, text8.stringWidth("Thắng: ") + DX, DY, GameConstants.TOP_LEFT_ANCHOR);
          text8.drawString2(g, usrInfo.point, GameColor.WHITE_CODE, text8.stringWidth("Thắng: ") + DX + text8.stringWidth(usrInfo.user) + 15, DY, GameConstants.TOP_LEFT_ANCHOR);
          text8.drawString2(g, "-" + String.valueOf(usrInfo.bet) + " gold", GameColor.WHITE_CODE, text8.stringWidth("Thắng:") + DX + text8.stringWidth(usrInfo.user) + 15 + text8.stringWidth("10 nước")
              + 10, DY, GameConstants.TOP_LEFT_ANCHOR);
          DY = DY + 15;
        }
      }
    }
  }

  private void drawHaBai(Graphics g) {
    for (int i = 0; i < userInfoList.size(); i++) {
      UserInfo userInfo = (UserInfo) userInfoList.elementAt(i);
      String position = userInfo.position;
      if (userInfo.status == 2) {
        if (position.equals("LeftCenter")) {
          g.drawImage(getCardImage(userInfo.card.array[2].cardName), 0, GameConstants.SCREEN_HEIGHT / 2 - imgBaiUp.getHeight() / 2, GameConstants.TOP_LEFT_ANCHOR);
          g.drawImage(getCardImage(userInfo.card.array[1].cardName), 15, GameConstants.SCREEN_HEIGHT / 2 - imgBaiUp.getHeight() / 2, GameConstants.TOP_LEFT_ANCHOR);
          g.drawImage(getCardImage(userInfo.card.array[0].cardName), 29, GameConstants.SCREEN_HEIGHT / 2 - imgBaiUp.getHeight() / 2, GameConstants.TOP_LEFT_ANCHOR);
        } else if (position.equals("RightCenter")) {
          g.drawImage(getCardImage(userInfo.card.array[0].cardName), GameConstants.SCREEN_WIDTH - 29, GameConstants.SCREEN_HEIGHT / 2 - imgBaiUp.getHeight() / 2, GameConstants.TOP_RIGHT_ANCHOR);
          g.drawImage(getCardImage(userInfo.card.array[1].cardName), GameConstants.SCREEN_WIDTH - 15, GameConstants.SCREEN_HEIGHT / 2 - imgBaiUp.getHeight() / 2, GameConstants.TOP_RIGHT_ANCHOR);
          g.drawImage(getCardImage(userInfo.card.array[2].cardName), GameConstants.SCREEN_WIDTH, GameConstants.SCREEN_HEIGHT / 2 - imgBaiUp.getHeight() / 2, GameConstants.TOP_RIGHT_ANCHOR);
        } else if (position.equals("TopLeft")) {
          g.drawImage(getCardImage(userInfo.card.array[0].cardName), 31, 0 - imgBaiUp.getHeight() / 2, GameConstants.TOP_LEFT_ANCHOR);
          g.drawImage(getCardImage(userInfo.card.array[1].cardName), 17, 0 - imgBaiUp.getHeight() / 2, GameConstants.TOP_LEFT_ANCHOR);
          g.drawImage(getCardImage(userInfo.card.array[2].cardName), 0, 0 - imgBaiUp.getHeight() / 2, GameConstants.TOP_LEFT_ANCHOR);
        } else if (position.equals("TopRight")) {
          g.drawImage(getCardImage(userInfo.card.array[2].cardName), GameConstants.SCREEN_WIDTH, 0 - imgBaiUp.getHeight() / 2, GameConstants.TOP_RIGHT_ANCHOR);
          g.drawImage(getCardImage(userInfo.card.array[1].cardName), GameConstants.SCREEN_WIDTH - 17, 0 - imgBaiUp.getHeight() / 2, GameConstants.TOP_RIGHT_ANCHOR);
          g.drawImage(getCardImage(userInfo.card.array[0].cardName), GameConstants.SCREEN_WIDTH - 31, 0 - imgBaiUp.getHeight() / 2, GameConstants.TOP_RIGHT_ANCHOR);
        } else if (position.equals("BottomLeft")) {
          g.drawImage(getCardImage(userInfo.card.array[0].cardName), 0, (GameConstants.SCREEN_HEIGHT - DrawUtils.getBottomBarHeight()) - imgBaiUp.getHeight() / 2, GameConstants.TOP_LEFT_ANCHOR);
          g.drawImage(getCardImage(userInfo.card.array[1].cardName), 15, (GameConstants.SCREEN_HEIGHT - DrawUtils.getBottomBarHeight()) - imgBaiUp.getHeight() / 2, GameConstants.TOP_LEFT_ANCHOR);
          g.drawImage(getCardImage(userInfo.card.array[2].cardName), 29, (GameConstants.SCREEN_HEIGHT - DrawUtils.getBottomBarHeight()) - imgBaiUp.getHeight() / 2, GameConstants.TOP_LEFT_ANCHOR);
        } else if (position.equals("BottomRight")) {
          g.drawImage(getCardImage(userInfo.card.array[2].cardName), GameConstants.SCREEN_WIDTH - 29, (GameConstants.SCREEN_HEIGHT - DrawUtils.getBottomBarHeight()) - imgBaiUp.getHeight() / 2,
              GameConstants.TOP_RIGHT_ANCHOR);
          g.drawImage(getCardImage(userInfo.card.array[1].cardName), GameConstants.SCREEN_WIDTH - 15, (GameConstants.SCREEN_HEIGHT - DrawUtils.getBottomBarHeight()) - imgBaiUp.getHeight() / 2,
              GameConstants.TOP_RIGHT_ANCHOR);
          g.drawImage(getCardImage(userInfo.card.array[0].cardName), GameConstants.SCREEN_WIDTH, (GameConstants.SCREEN_HEIGHT - DrawUtils.getBottomBarHeight()) - imgBaiUp.getHeight() / 2,
              GameConstants.TOP_RIGHT_ANCHOR);
        }
      } else if (userInfo.status == VIEW_USER_STATUS) {
        GameImage avatar = Global.getAvatar(userInfo.user);
        if (avatar == null) {
          avatar = Global.getDefaultAvatar();
        }
        if (position.equals("LeftCenter")) {
          g.drawImage(avatar, 0, GameConstants.SCREEN_HEIGHT / 2 - defaultAvatar.getHeight(), GameConstants.TOP_LEFT_ANCHOR);
        } else if (position.equals("RightCenter")) {
          g.drawImage(avatar, GameConstants.SCREEN_WIDTH, GameConstants.SCREEN_HEIGHT / 2 - defaultAvatar.getHeight(), GameConstants.TOP_RIGHT_ANCHOR);
        } else if (position.equals("TopLeft")) {
          g.drawImage(avatar, 0, 0, GameConstants.TOP_LEFT_ANCHOR);
        } else if (position.equals("TopRight")) {
          g.drawImage(avatar, GameConstants.SCREEN_WIDTH, 0, GameConstants.TOP_RIGHT_ANCHOR);
        } else if (position.equals("BottomLeft")) {
          g.drawImage(avatar, 0, (GameConstants.SCREEN_HEIGHT - DrawUtils.getBottomBarHeight()) - defaultAvatar.getHeight(), GameConstants.TOP_LEFT_ANCHOR);
        } else if (position.equals("BottomRight")) {
          g.drawImage(avatar, GameConstants.SCREEN_WIDTH, ((GameConstants.SCREEN_HEIGHT - DrawUtils.getBottomBarHeight()) - defaultAvatar.getHeight()), GameConstants.TOP_RIGHT_ANCHOR);
        }
      }
    }
  }

  private void drawPrepare(Graphics g) {
    for (int i = 0; i < userInfoList.size(); i++) {
      UserInfo userInfo = (UserInfo) userInfoList.elementAt(i);
      String position = userInfo.position;
      GameImage avatar = Global.getAvatar(userInfo.user);
      if (avatar == null) {
        avatar = Global.getDefaultAvatar();
      }
      if (position.equals("TopLeft")) {
        g.drawImage(avatar, 0, 0, GameConstants.TOP_LEFT_ANCHOR);
        text8.drawString2(g, userInfo.user, GameColor.WHITE_CODE, 0, defaultAvatar.getHeight(), GameConstants.TOP_LEFT_ANCHOR);
        text8.drawString2(g, userInfo.bet + " gold", GameColor.WHITE_CODE, defaultAvatar.getWidth(), defaultAvatar.getHeight(), GameConstants.TOP_LEFT_ANCHOR);
        if (userInfo.master == 1 && userInfo.status != 4) {
          g.drawImage(dollarImage, defaultAvatar.getWidth(), 0, GameConstants.TOP_LEFT_ANCHOR);
        }
        if (isFinish && userInfo.showNotifyHaBaiCount < MAX_SHOW_NOTIFY_COUNT) {
          g.drawImage(notifyEndGameImage2, 25, 0, GameConstants.TOP_LEFT_ANCHOR);
          String winString = "Thua";
          StringBuffer sbMoney = new StringBuffer();
          if (userInfo.win == 1) {
            winString = "Thắng";
            sbMoney.append("+");
            sbMoney.append(userInfo.bet);
            sbMoney.append(" ");
            sbMoney.append("gold");
          } else {
            winString = "Thua";
            sbMoney.append("-");
            sbMoney.append(userInfo.bet);
            sbMoney.append(" ");
            sbMoney.append("gold");
          }
          if (userInfo.status != 4) {
            text8.drawString2(g, winString, GameColor.WHITE_CODE, 37, 0 + 10, GameConstants.TOP_LEFT_ANCHOR);
            text8.drawString2(g, sbMoney.toString(), GameColor.WHITE_CODE, 37, 0 + 40, GameConstants.TOP_LEFT_ANCHOR);
            userInfo.showNotifyHaBaiCount = userInfo.showNotifyHaBaiCount + 1;
          }
        }
      }
      if (position.equals("TopRight")) {
        g.drawImage(avatar, GameConstants.SCREEN_WIDTH, 0, GameConstants.TOP_RIGHT_ANCHOR);
        text8.drawString2(g, userInfo.user, GameColor.WHITE_CODE, GameConstants.SCREEN_WIDTH, defaultAvatar.getHeight(), GameConstants.TOP_RIGHT_ANCHOR);
        text8.drawString2(g, userInfo.bet + " gold", GameColor.WHITE_CODE, (GameConstants.SCREEN_WIDTH - defaultAvatar.getWidth()) + 10, defaultAvatar.getHeight(), GameConstants.TOP_RIGHT_ANCHOR);
        if (userInfo.master == 1 && userInfo.status != 4) {
          g.drawImage(dollarImage, (GameConstants.SCREEN_WIDTH - defaultAvatar.getWidth()), 0, GameConstants.TOP_RIGHT_ANCHOR);
        }
        if (isFinish && userInfo.showNotifyHaBaiCount < MAX_SHOW_NOTIFY_COUNT) {
          g.drawImage(notifyEndGameImage2, GameConstants.SCREEN_WIDTH - 25, 0, GameConstants.TOP_RIGHT_ANCHOR);
          String winString = "Thua";
          StringBuffer sbMoney = new StringBuffer();
          if (userInfo.win == 1) {
            winString = "Thắng";
            sbMoney.append("+");
            sbMoney.append(userInfo.bet);
            sbMoney.append(" ");
            sbMoney.append("gold");
          } else {
            winString = "Thua";
            sbMoney.append("-");
            sbMoney.append(userInfo.bet);
            sbMoney.append(" ");
            sbMoney.append("gold");
          }
          if (userInfo.status != 4) {
            text8.drawString2(g, winString, GameColor.WHITE_CODE, GameConstants.SCREEN_WIDTH - 37, 0 + 10, GameConstants.TOP_RIGHT_ANCHOR);
            text8.drawString2(g, sbMoney.toString(), GameColor.WHITE_CODE, GameConstants.SCREEN_WIDTH - 37, 0 + 40, GameConstants.TOP_RIGHT_ANCHOR);
            userInfo.showNotifyHaBaiCount = userInfo.showNotifyHaBaiCount + 1;
          }
        }
      }
      if (position.equals("LeftCenter")) {
        g.drawImage(avatar, 0, GameConstants.SCREEN_HEIGHT / 2 - defaultAvatar.getHeight(), GameConstants.TOP_LEFT_ANCHOR);
        text8.drawString2(g, userInfo.user, GameColor.WHITE_CODE, 0, (GameConstants.SCREEN_HEIGHT / 2), GameConstants.TOP_LEFT_ANCHOR);
        text8.drawString2(g, userInfo.bet + " gold", GameColor.WHITE_CODE, defaultAvatar.getWidth(), (GameConstants.SCREEN_HEIGHT / 2), GameConstants.TOP_LEFT_ANCHOR);
        if (userInfo.master == 1 && userInfo.status != 4) {
          g.drawImage(dollarImage, defaultAvatar.getWidth(), GameConstants.SCREEN_HEIGHT / 2 - defaultAvatar.getHeight(), GameConstants.TOP_LEFT_ANCHOR);
        }
        if (isFinish && userInfo.showNotifyHaBaiCount < MAX_SHOW_NOTIFY_COUNT) {
          // Hiển thị số điểm
          g.drawImage(notifyEndGameImage, 15, GameConstants.SCREEN_HEIGHT / 2 - imgBaiUp.getHeight() / 2, GameConstants.TOP_LEFT_ANCHOR);
          String winString = "Thua";
          StringBuffer sbMoney = new StringBuffer();
          if (userInfo.win == 1) {
            winString = "Thắng";
            sbMoney.append("+");
            sbMoney.append(userInfo.bet);
            sbMoney.append(" ");
            sbMoney.append("gold");
          } else {
            winString = "Thua";
            sbMoney.append("-");
            sbMoney.append(userInfo.bet);
            sbMoney.append(" ");
            sbMoney.append("gold");
          }
          if (userInfo.status != 4) {
            text8.drawString2(g, winString, GameColor.WHITE_CODE, 20, GameConstants.SCREEN_HEIGHT / 2 - imgBaiUp.getHeight() / 2, GameConstants.TOP_LEFT_ANCHOR);
            text8.drawString2(g, sbMoney.toString(), GameColor.WHITE_CODE, 20, (GameConstants.SCREEN_HEIGHT / 2 - imgBaiUp.getHeight() / 2) + 40, GameConstants.TOP_LEFT_ANCHOR);
            userInfo.showNotifyHaBaiCount = userInfo.showNotifyHaBaiCount + 1;
          }
        }
      }
      if (position.equals("RightCenter")) {
        g.drawImage(avatar, GameConstants.SCREEN_WIDTH, GameConstants.SCREEN_HEIGHT / 2 - defaultAvatar.getHeight(), GameConstants.TOP_RIGHT_ANCHOR);
        text8.drawString2(g, userInfo.user, GameColor.WHITE_CODE, GameConstants.SCREEN_WIDTH, (GameConstants.SCREEN_HEIGHT / 2), GameConstants.TOP_RIGHT_ANCHOR);
        text8.drawString2(g, userInfo.bet + " gold", GameColor.WHITE_CODE, (GameConstants.SCREEN_WIDTH - defaultAvatar.getWidth()) + 10, GameConstants.SCREEN_HEIGHT / 2, GameConstants.TOP_RIGHT_ANCHOR);
        if (userInfo.master == 1 && userInfo.status != 4) {
          g.drawImage(dollarImage, (GameConstants.SCREEN_WIDTH - defaultAvatar.getWidth()), GameConstants.SCREEN_HEIGHT / 2 - defaultAvatar.getHeight(), GameConstants.TOP_RIGHT_ANCHOR);
        }
        if (isFinish && userInfo.showNotifyHaBaiCount < MAX_SHOW_NOTIFY_COUNT) {
          g.drawImage(notifyEndGameImage, GameConstants.SCREEN_WIDTH - 15, GameConstants.SCREEN_HEIGHT / 2 - imgBaiUp.getHeight() / 2, GameConstants.TOP_RIGHT_ANCHOR);
          String winString = "Thua";
          StringBuffer sbMoney = new StringBuffer();
          if (userInfo.win == 1) {
            winString = "Thắng";
            sbMoney.append("+");
            sbMoney.append(userInfo.bet);
            sbMoney.append(" ");
            sbMoney.append("gold");
          } else {
            winString = "Thua";
            sbMoney.append("-");
            sbMoney.append(userInfo.bet);
            sbMoney.append(" ");
            sbMoney.append("gold");
          }
          if (userInfo.status != 4) {
            text8.drawString2(g, winString, GameColor.WHITE_CODE, GameConstants.SCREEN_WIDTH - 20, GameConstants.SCREEN_HEIGHT / 2 - imgBaiUp.getHeight() / 2, GameConstants.TOP_RIGHT_ANCHOR);
            text8.drawString2(g, sbMoney.toString(), GameColor.WHITE_CODE, GameConstants.SCREEN_WIDTH - 20, (GameConstants.SCREEN_HEIGHT / 2 - imgBaiUp.getHeight() / 2) + 40,
                GameConstants.TOP_RIGHT_ANCHOR);
            userInfo.showNotifyHaBaiCount = userInfo.showNotifyHaBaiCount + 1;
          }
        }
      }
      if (position.equals("BottomLeft")) {
        g.drawImage(avatar, 0, (GameConstants.SCREEN_HEIGHT - DrawUtils.getBottomBarHeight()) - defaultAvatar.getHeight(), GameConstants.TOP_LEFT_ANCHOR);
        text8.drawString2(g, userInfo.user, GameColor.WHITE_CODE, 0, ((GameConstants.SCREEN_HEIGHT - DrawUtils.getBottomBarHeight()) - defaultAvatar.getHeight()) - 10, GameConstants.TOP_LEFT_ANCHOR);
        text8.drawString2(g, userInfo.bet + " gold", GameColor.WHITE_CODE, defaultAvatar.getWidth(), ((GameConstants.SCREEN_HEIGHT - DrawUtils.getBottomBarHeight()) - defaultAvatar.getHeight()) - 10,
            GameConstants.TOP_LEFT_ANCHOR);
        if (userInfo.master == 1 && userInfo.status != 4) {
          g.drawImage(dollarImage, defaultAvatar.getWidth(), (GameConstants.SCREEN_HEIGHT - DrawUtils.getBottomBarHeight()) - defaultAvatar.getHeight(), GameConstants.TOP_LEFT_ANCHOR);
        }
        if (isFinish && userInfo.showNotifyHaBaiCount < MAX_SHOW_NOTIFY_COUNT) {
          g.drawImage(notifyEndGameImage, 21, (GameConstants.SCREEN_HEIGHT - DrawUtils.getBottomBarHeight()) - imgBaiUp.getHeight() + 10, GameConstants.TOP_LEFT_ANCHOR);
          String winString = "Thua";
          StringBuffer sbMoney = new StringBuffer();
          if (userInfo.win == 1) {
            winString = "Thắng";
            sbMoney.append("+");
            sbMoney.append(userInfo.bet);
            sbMoney.append(" ");
            sbMoney.append("gold");
          } else {
            winString = "Thua";
            sbMoney.append("-");
            sbMoney.append(userInfo.bet);
            sbMoney.append(" ");
            sbMoney.append("gold");
          }
          if (userInfo.status != 4) {
            text8.drawString2(g, winString, GameColor.WHITE_CODE, 29, (GameConstants.SCREEN_HEIGHT - DrawUtils.getBottomBarHeight()) - imgBaiUp.getHeight() + 10, GameConstants.TOP_LEFT_ANCHOR);
            text8.drawString2(g, sbMoney.toString(), GameColor.WHITE_CODE, 29, ((GameConstants.SCREEN_HEIGHT - DrawUtils.getBottomBarHeight()) - imgBaiUp.getHeight()) + 10 + 30,
                GameConstants.TOP_LEFT_ANCHOR);
            userInfo.showNotifyHaBaiCount = userInfo.showNotifyHaBaiCount + 1;
          }
        }
      }
      if (position.equals("BottomRight")) {
        g.drawImage(avatar, GameConstants.SCREEN_WIDTH, ((GameConstants.SCREEN_HEIGHT - DrawUtils.getBottomBarHeight()) - defaultAvatar.getHeight()), GameConstants.TOP_RIGHT_ANCHOR);
        text8.drawString2(g, userInfo.user, GameColor.WHITE_CODE, GameConstants.SCREEN_WIDTH, ((GameConstants.SCREEN_HEIGHT - DrawUtils.getBottomBarHeight()) - defaultAvatar.getHeight()) - 10,
            GameConstants.TOP_RIGHT_ANCHOR);
        text8.drawString2(g, userInfo.bet + " gold", GameColor.WHITE_CODE, (GameConstants.SCREEN_WIDTH - defaultAvatar.getWidth()) + 10,
            ((GameConstants.SCREEN_HEIGHT - DrawUtils.getBottomBarHeight()) - defaultAvatar.getHeight()) - 10, GameConstants.TOP_RIGHT_ANCHOR);
        if (userInfo.master == 1 && userInfo.status != 4) {
          g.drawImage(dollarImage, (GameConstants.SCREEN_WIDTH - defaultAvatar.getWidth()), (GameConstants.SCREEN_HEIGHT - DrawUtils.getBottomBarHeight()) - defaultAvatar.getHeight(),
              GameConstants.TOP_RIGHT_ANCHOR);
        }
        if (isFinish && userInfo.showNotifyHaBaiCount < MAX_SHOW_NOTIFY_COUNT) {
          g.drawImage(notifyEndGameImage, GameConstants.SCREEN_WIDTH - 21, (GameConstants.SCREEN_HEIGHT - DrawUtils.getBottomBarHeight()) - imgBaiUp.getHeight() + 10, GameConstants.TOP_RIGHT_ANCHOR);
          String winString = "Thua";
          StringBuffer sbMoney = new StringBuffer();
          if (userInfo.win == 1) {
            winString = "Thắng";
            sbMoney.append("+");
            sbMoney.append(userInfo.bet);
            sbMoney.append(" ");
            sbMoney.append("gold");
          } else {
            winString = "Thua";
            sbMoney.append("-");
            sbMoney.append(userInfo.bet);
            sbMoney.append(" ");
            sbMoney.append("gold");
          }
          if (userInfo.status != 4) {
            text8.drawString2(g, winString, GameColor.WHITE_CODE, GameConstants.SCREEN_WIDTH - 29, (GameConstants.SCREEN_HEIGHT - DrawUtils.getBottomBarHeight()) - imgBaiUp.getHeight() + 10,
                GameConstants.TOP_RIGHT_ANCHOR);
            text8.drawString2(g, sbMoney.toString(), GameColor.WHITE_CODE, GameConstants.SCREEN_WIDTH - 29, (GameConstants.SCREEN_HEIGHT - DrawUtils.getBottomBarHeight()) - imgBaiUp.getHeight() + 10 + 30,
                GameConstants.TOP_RIGHT_ANCHOR);
            userInfo.showNotifyHaBaiCount = userInfo.showNotifyHaBaiCount + 1;
          }
        }
      }
    }
  }

  private void drawChiaBai(Graphics g) {
    for (int i = 0; i < userInfoList.size(); i++) {
      UserInfo userInfo = (UserInfo) userInfoList.elementAt(i);
      if (userInfo == null)
        continue;
      String position = userInfo.position;
      if (userInfo.status != VIEW_USER_STATUS) {
        if (position.equals("LeftCenter")) {
          g.drawImage(imgBaiUp, 0, GameConstants.SCREEN_HEIGHT / 2 - imgBaiUp.getHeight() / 2, GameConstants.TOP_LEFT_ANCHOR);
          g.drawImage(imgBaiUp, 15, GameConstants.SCREEN_HEIGHT / 2 - imgBaiUp.getHeight() / 2, GameConstants.TOP_LEFT_ANCHOR);
          g.drawImage(imgBaiUp, 29, GameConstants.SCREEN_HEIGHT / 2 - imgBaiUp.getHeight() / 2, GameConstants.TOP_LEFT_ANCHOR);
        } else if (position.equals("RightCenter")) {
          g.drawImage(imgBaiUp, GameConstants.SCREEN_WIDTH, GameConstants.SCREEN_HEIGHT / 2 - imgBaiUp.getHeight() / 2, GameConstants.TOP_RIGHT_ANCHOR);
          g.drawImage(imgBaiUp, GameConstants.SCREEN_WIDTH - 15, GameConstants.SCREEN_HEIGHT / 2 - imgBaiUp.getHeight() / 2, GameConstants.TOP_RIGHT_ANCHOR);
          g.drawImage(imgBaiUp, GameConstants.SCREEN_WIDTH - 29, GameConstants.SCREEN_HEIGHT / 2 - imgBaiUp.getHeight() / 2, GameConstants.TOP_RIGHT_ANCHOR);
        } else if (position.equals("TopLeft")) {
          g.drawImage(imgBaiUp, 0, 0 - imgBaiUp.getHeight() / 2, GameConstants.TOP_LEFT_ANCHOR);
          g.drawImage(imgBaiUp, 17, 0 - imgBaiUp.getHeight() / 2, GameConstants.TOP_LEFT_ANCHOR);
          g.drawImage(imgBaiUp, 31, 0 - imgBaiUp.getHeight() / 2, GameConstants.TOP_LEFT_ANCHOR);
        } else if (position.equals("TopRight")) {
          g.drawImage(imgBaiUp, GameConstants.SCREEN_WIDTH, 0 - imgBaiUp.getHeight() / 2, GameConstants.TOP_RIGHT_ANCHOR);
          g.drawImage(imgBaiUp, GameConstants.SCREEN_WIDTH - 17, 0 - imgBaiUp.getHeight() / 2, GameConstants.TOP_RIGHT_ANCHOR);
          g.drawImage(imgBaiUp, GameConstants.SCREEN_WIDTH - 31, 0 - imgBaiUp.getHeight() / 2, GameConstants.TOP_RIGHT_ANCHOR);
        } else if (position.equals("BottomLeft")) {
          g.drawImage(imgBaiUp, 0, (GameConstants.SCREEN_HEIGHT - DrawUtils.getBottomBarHeight()) - imgBaiUp.getHeight() / 2, GameConstants.TOP_LEFT_ANCHOR);
          g.drawImage(imgBaiUp, 15, (GameConstants.SCREEN_HEIGHT - DrawUtils.getBottomBarHeight()) - imgBaiUp.getHeight() / 2, GameConstants.TOP_LEFT_ANCHOR);
          g.drawImage(imgBaiUp, 29, (GameConstants.SCREEN_HEIGHT - DrawUtils.getBottomBarHeight()) - imgBaiUp.getHeight() / 2, GameConstants.TOP_LEFT_ANCHOR);
        } else if (position.equals("BottomRight")) {
          g.drawImage(imgBaiUp, GameConstants.SCREEN_WIDTH, (GameConstants.SCREEN_HEIGHT - DrawUtils.getBottomBarHeight()) - imgBaiUp.getHeight() / 2, GameConstants.TOP_RIGHT_ANCHOR);
          g.drawImage(imgBaiUp, GameConstants.SCREEN_WIDTH - 15, (GameConstants.SCREEN_HEIGHT - DrawUtils.getBottomBarHeight()) - imgBaiUp.getHeight() / 2, GameConstants.TOP_RIGHT_ANCHOR);
          g.drawImage(imgBaiUp, GameConstants.SCREEN_WIDTH - 29, (GameConstants.SCREEN_HEIGHT - DrawUtils.getBottomBarHeight()) - imgBaiUp.getHeight() / 2, GameConstants.TOP_RIGHT_ANCHOR);
        }
      } else {
        GameImage avatar = Global.getAvatar(userInfo.user);
        if (avatar == null) {
          avatar = Global.getDefaultAvatar();
        }
        if (position.equals("LeftCenter")) {
          g.drawImage(avatar, 0, GameConstants.SCREEN_HEIGHT / 2 - defaultAvatar.getHeight(), GameConstants.TOP_LEFT_ANCHOR);
          text8.drawString2(g, userInfo.user, GameColor.WHITE_CODE, 0, (GameConstants.SCREEN_HEIGHT / 2), GameConstants.TOP_LEFT_ANCHOR);
        } else if (position.equals("RightCenter")) {
          g.drawImage(avatar, GameConstants.SCREEN_WIDTH, GameConstants.SCREEN_HEIGHT / 2 - defaultAvatar.getHeight(), GameConstants.TOP_RIGHT_ANCHOR);
          text8.drawString2(g, userInfo.user, GameColor.WHITE_CODE, GameConstants.SCREEN_WIDTH, (GameConstants.SCREEN_HEIGHT / 2), GameConstants.TOP_RIGHT_ANCHOR);
        } else if (position.equals("TopLeft")) {
          g.drawImage(avatar, 0, 0, GameConstants.TOP_LEFT_ANCHOR);
          text8.drawString2(g, userInfo.user, GameColor.WHITE_CODE, 0, defaultAvatar.getHeight(), GameConstants.TOP_LEFT_ANCHOR);
        } else if (position.equals("TopRight")) {
          g.drawImage(avatar, GameConstants.SCREEN_WIDTH, 0, GameConstants.TOP_RIGHT_ANCHOR);
          text8.drawString2(g, userInfo.user, GameColor.WHITE_CODE, GameConstants.SCREEN_WIDTH, defaultAvatar.getHeight(), GameConstants.TOP_RIGHT_ANCHOR);
        } else if (position.equals("BottomLeft")) {
          g.drawImage(avatar, 0, (GameConstants.SCREEN_HEIGHT - DrawUtils.getBottomBarHeight()) - defaultAvatar.getHeight(), GameConstants.TOP_LEFT_ANCHOR);
          text8.drawString2(g, userInfo.user, GameColor.WHITE_CODE, 0, ((GameConstants.SCREEN_HEIGHT - DrawUtils.getBottomBarHeight()) - defaultAvatar.getHeight()) - 10, GameConstants.TOP_LEFT_ANCHOR);
        } else if (position.equals("BottomRight")) {
          g.drawImage(avatar, GameConstants.SCREEN_WIDTH, ((GameConstants.SCREEN_HEIGHT - DrawUtils.getBottomBarHeight()) - defaultAvatar.getHeight()), GameConstants.TOP_RIGHT_ANCHOR);
          text8.drawString2(g, userInfo.user, GameColor.WHITE_CODE, GameConstants.SCREEN_WIDTH, ((GameConstants.SCREEN_HEIGHT - DrawUtils.getBottomBarHeight()) - defaultAvatar.getHeight()) - 10,
              GameConstants.TOP_RIGHT_ANCHOR);
        }
      }
    }
  }

  private void drawMenu(UserInfo userInfo, Graphics g) {
    if (userInfo == null) {
      return;
    }

    if (!isBet) {
      if (userInfo.master != 1 && userInfo.status != VIEW_USER_STATUS) {
        DrawUtils.setBottomBar(_manager, "Thoát", "Cược", "Xóa");
      }
    } else {
      if (!isFinish && canCommit(userInfo)) {
        DrawUtils.setBottomBar(_manager, "Thoát", "Hạ bài", "");
      } else if (!isFinish && userInfo.status == 0) {
        DrawUtils.setBottomBar(_manager, "Thoát", "Lật", "Lật hết");
      } else if (userInfo.status == 2) {
        DrawUtils.getBottomBar(_manager).setCenterLabel("Chờ");
      }
    }

    if (isFinish) {
      DrawUtils.setBottomBar(_manager, "Thoát", "", "Chơi tiếp");
    }
  }

  private void drawLatBai(UserInfo userInfo, Graphics g) {
    if (userInfo == null || userInfo.status == VIEW_USER_STATUS) {
      return;
    }
    if (userInfo.position.equals("LeftCenter")) {
      if (userInfo.card.array[2].isOpen) {
        g.drawImage(getCardImage(userInfo.card.array[1].cardName), 0, GameConstants.SCREEN_HEIGHT / 2 - imgBaiUp.getHeight() / 2, GameConstants.TOP_LEFT_ANCHOR);
      }
      if (userInfo.card.array[1].isOpen) {
        g.drawImage(getCardImage(userInfo.card.array[0].cardName), 15, GameConstants.SCREEN_HEIGHT / 2 - imgBaiUp.getHeight() / 2, GameConstants.TOP_LEFT_ANCHOR);
      }
      if (userInfo.card.array[0].isOpen) {
        g.drawImage(getCardImage(userInfo.card.array[2].cardName), 29, GameConstants.SCREEN_HEIGHT / 2 - imgBaiUp.getHeight() / 2, GameConstants.TOP_LEFT_ANCHOR);
      }
      if (userInfo.status == 1 && !isFinish && userInfo.showNotifyLatBaiCount < MAX_SHOW_NOTIFY_COUNT) {
        g.drawImage(notifyImage, 29, (GameConstants.SCREEN_HEIGHT / 2 - imgBaiUp.getHeight() / 2) + 20, GameConstants.TOP_LEFT_ANCHOR);
        text8.drawString2(g, String.valueOf(userInfo.point), GameColor.WHITE_CODE, 32, (GameConstants.SCREEN_HEIGHT / 2 - imgBaiUp.getHeight() / 2) + 20, GameConstants.TOP_LEFT_ANCHOR);
        userInfo.showNotifyLatBaiCount = userInfo.showNotifyLatBaiCount + 1;
      }
    }
    if (userInfo.position.equals("RightCenter")) {
      if (userInfo.card.array[0].isOpen) {
        g.drawImage(getCardImage(userInfo.card.array[0].cardName), GameConstants.SCREEN_WIDTH - 29, GameConstants.SCREEN_HEIGHT / 2 - imgBaiUp.getHeight() / 2, GameConstants.TOP_RIGHT_ANCHOR);
      }
      if (userInfo.card.array[1].isOpen) {
        g.drawImage(getCardImage(userInfo.card.array[1].cardName), GameConstants.SCREEN_WIDTH - 15, GameConstants.SCREEN_HEIGHT / 2 - imgBaiUp.getHeight() / 2, GameConstants.TOP_RIGHT_ANCHOR);
      }
      if (userInfo.card.array[2].isOpen) {
        g.drawImage(getCardImage(userInfo.card.array[2].cardName), GameConstants.SCREEN_WIDTH, GameConstants.SCREEN_HEIGHT / 2 - imgBaiUp.getHeight() / 2, GameConstants.TOP_RIGHT_ANCHOR);
      }
      if (userInfo.status == 1 && !isFinish && userInfo.showNotifyLatBaiCount < MAX_SHOW_NOTIFY_COUNT) {
        g.drawImage(notifyImage, GameConstants.SCREEN_WIDTH - 29, (GameConstants.SCREEN_HEIGHT / 2 - imgBaiUp.getHeight() / 2) + 20, GameConstants.TOP_RIGHT_ANCHOR);
        text8.drawString2(g, String.valueOf(userInfo.point), GameColor.WHITE_CODE, GameConstants.SCREEN_WIDTH - imgBaiUp.getHeight() / 2, (GameConstants.SCREEN_HEIGHT / 2 - imgBaiUp.getHeight() / 2) + 20,
            GameConstants.TOP_RIGHT_ANCHOR);
        userInfo.showNotifyLatBaiCount = userInfo.showNotifyLatBaiCount + 1;
      }
    }
    if (userInfo.position.equals("TopLeft")) {
      if (userInfo.card.array[0].isOpen) {
        g.drawImage(getCardImage(userInfo.card.array[0].cardName), 31, 0 - imgBaiUp.getHeight() / 2, GameConstants.TOP_LEFT_ANCHOR);
      }
      if (userInfo.card.array[1].isOpen) {
        g.drawImage(getCardImage(userInfo.card.array[1].cardName), 17, 0 - imgBaiUp.getHeight() / 2, GameConstants.TOP_LEFT_ANCHOR);
      }
      if (userInfo.card.array[2].isOpen) {
        g.drawImage(getCardImage(userInfo.card.array[2].cardName), 0, 0 - imgBaiUp.getHeight() / 2, GameConstants.TOP_LEFT_ANCHOR);
      }
      if (userInfo.status == 1 && !isFinish && userInfo.showNotifyLatBaiCount < MAX_SHOW_NOTIFY_COUNT) {
        g.drawImage(notifyImage2, 10, (0 + imgBaiUp.getHeight() / 2), GameConstants.TOP_LEFT_ANCHOR);
        text8.drawString2(g, String.valueOf(userInfo.point), GameColor.WHITE_CODE, 13, (0 + imgBaiUp.getHeight() / 2) + 3, GameConstants.TOP_LEFT_ANCHOR);
        userInfo.showNotifyLatBaiCount = userInfo.showNotifyLatBaiCount + 1;
      }
    }
    if (userInfo.position.equals("TopRight")) {
      if (userInfo.card.array[2].isOpen) {
        g.drawImage(getCardImage(userInfo.card.array[2].cardName), GameConstants.SCREEN_WIDTH, 0 - imgBaiUp.getHeight() / 2, GameConstants.TOP_RIGHT_ANCHOR);
      }
      if (userInfo.card.array[1].isOpen) {
        g.drawImage(getCardImage(userInfo.card.array[1].cardName), GameConstants.SCREEN_WIDTH - 17, 0 - imgBaiUp.getHeight() / 2, GameConstants.TOP_RIGHT_ANCHOR);
      }
      if (userInfo.card.array[0].isOpen) {
        g.drawImage(getCardImage(userInfo.card.array[0].cardName), GameConstants.SCREEN_WIDTH - 31, 0 - imgBaiUp.getHeight() / 2, GameConstants.TOP_RIGHT_ANCHOR);
      }
      if (userInfo.status == 1 && !isFinish && userInfo.showNotifyLatBaiCount < MAX_SHOW_NOTIFY_COUNT) {
        g.drawImage(notifyImage2, GameConstants.SCREEN_WIDTH - 10, (0 + imgBaiUp.getHeight() / 2), GameConstants.TOP_RIGHT_ANCHOR);
        text8.drawString2(g, String.valueOf(userInfo.point), GameColor.WHITE_CODE, GameConstants.SCREEN_WIDTH - 13, (0 + imgBaiUp.getHeight() / 2) + 3, GameConstants.TOP_RIGHT_ANCHOR);
        userInfo.showNotifyLatBaiCount = userInfo.showNotifyLatBaiCount + 1;
      }
    }
    if (userInfo.position.equals("BottomLeft")) {
      if (userInfo.card.array[2].isOpen) {
        g.drawImage(getCardImage(userInfo.card.array[0].cardName), 0, (GameConstants.SCREEN_HEIGHT - DrawUtils.getBottomBarHeight()) - imgBaiUp.getHeight() / 2, GameConstants.TOP_LEFT_ANCHOR);
      }
      if (userInfo.card.array[1].isOpen) {
        g.drawImage(getCardImage(userInfo.card.array[1].cardName), 15, (GameConstants.SCREEN_HEIGHT - DrawUtils.getBottomBarHeight()) - imgBaiUp.getHeight() / 2, GameConstants.TOP_LEFT_ANCHOR);
      }
      if (userInfo.card.array[0].isOpen) {
        g.drawImage(getCardImage(userInfo.card.array[2].cardName), 29, (GameConstants.SCREEN_HEIGHT - DrawUtils.getBottomBarHeight()) - imgBaiUp.getHeight() / 2, GameConstants.TOP_LEFT_ANCHOR);
      }
      if (canCommit(userInfo) && !isFinish && userInfo.showNotifyLatBaiCount < MAX_SHOW_NOTIFY_COUNT) {
        g.drawImage(notifyImage, 14, ((GameConstants.SCREEN_HEIGHT - DrawUtils.getBottomBarHeight()) - imgBaiUp.getHeight() / 2) - 20, GameConstants.TOP_LEFT_ANCHOR);
        text8.drawString2(g, String.valueOf(userInfo.point), GameColor.WHITE_CODE, 21, ((GameConstants.SCREEN_HEIGHT - DrawUtils.getBottomBarHeight()) - imgBaiUp.getHeight() / 2) - 20,
            GameConstants.TOP_LEFT_ANCHOR);
        userInfo.showNotifyLatBaiCount = userInfo.showNotifyLatBaiCount + 1;
      }
    }
    if (userInfo.position.equals("BottomRight")) {
      if (userInfo.card.array[0].isOpen) {
        g.drawImage(getCardImage(userInfo.card.array[0].cardName), GameConstants.SCREEN_WIDTH - 29, (GameConstants.SCREEN_HEIGHT - DrawUtils.getBottomBarHeight()) - imgBaiUp.getHeight() / 2,
            GameConstants.TOP_RIGHT_ANCHOR);
      }
      if (userInfo.card.array[1].isOpen) {
        g.drawImage(getCardImage(userInfo.card.array[1].cardName), GameConstants.SCREEN_WIDTH - 15, (GameConstants.SCREEN_HEIGHT - DrawUtils.getBottomBarHeight()) - imgBaiUp.getHeight() / 2,
            GameConstants.TOP_RIGHT_ANCHOR);
      }
      if (userInfo.card.array[2].isOpen) {
        g.drawImage(getCardImage(userInfo.card.array[2].cardName), GameConstants.SCREEN_WIDTH, (GameConstants.SCREEN_HEIGHT - DrawUtils.getBottomBarHeight()) - imgBaiUp.getHeight() / 2,
            GameConstants.TOP_RIGHT_ANCHOR);
      }
      if (userInfo.status == 1 && !isFinish && userInfo.showNotifyLatBaiCount < MAX_SHOW_NOTIFY_COUNT) {
        g.drawImage(notifyImage, GameConstants.SCREEN_WIDTH - 14, ((GameConstants.SCREEN_HEIGHT - DrawUtils.getBottomBarHeight()) - imgBaiUp.getHeight() / 2) - 20, GameConstants.TOP_RIGHT_ANCHOR);
        text8.drawString2(g, String.valueOf(userInfo.point), GameColor.WHITE_CODE, GameConstants.SCREEN_WIDTH - 21,
            ((GameConstants.SCREEN_HEIGHT - DrawUtils.getBottomBarHeight()) - imgBaiUp.getHeight() / 2) - 20, GameConstants.TOP_RIGHT_ANCHOR);
        userInfo.showNotifyLatBaiCount = userInfo.showNotifyLatBaiCount + 1;
      }
    }
  }

  public void onConnectDone() {
  }

  public void onConnectFail() {
  }

  public void onDisconnect() {
  }

  private UserInfo findUserInfoByUserName(String userName) {
    for (int i = 0; i < userInfoList.size(); i++) {
      UserInfo userInfo = (UserInfo) userInfoList.elementAt(i);
      if (userInfo.user.equals(userName)) {
        return userInfo;
      }
    }
    return null;
  }

  private void parseResponseToUserInfo(String s) {
    StringArray arrResponse = GameStringUtils.split(s, "&");
    if (arrResponse == null || arrResponse.len() == 0) {
      return;
    }
    for (int i = 0; i < arrResponse.len(); i++) {
      StringArray usersInfoParam = GameStringUtils.split(arrResponse.get(i), ",");
      UserInfo userInfo = findUserInfoByUserName(usersInfoParam.get(0));
      if (userInfo == null) {
        userInfo = new UserInfo();
      }
      userInfo.user = usersInfoParam.get(0);
      userInfo.position = usersInfoParam.get(1);
      userInfo.status = NumberUtil.parseInt(usersInfoParam.get(3));
      userInfo.point = usersInfoParam.get(4);
      userInfo.win = NumberUtil.parseInt(usersInfoParam.get(5));
      userInfo.master = NumberUtil.parseInt(usersInfoParam.get(6));
      userInfo.bet = NumberUtil.parseInt(usersInfoParam.get(7));
      // Card
      StringArray cardInfo = GameStringUtils.split(usersInfoParam.get(2), ";");

      userInfo.card = new CardArray(new Card[cardInfo.len()], cardInfo.len());
      for (int j = 0; j < cardInfo.len(); j++) {
        StringArray arrCard = GameStringUtils.split(cardInfo.get(j), "@");
        userInfo.card.array[j] = new Card();
        userInfo.card.array[j].cardName = arrCard.get(0);
        if (arrCard.get(1).equalsIgnoreCase("true")) {
          userInfo.card.array[j].isOpen = true;
        } else {
          userInfo.card.array[j].isOpen = false;
        }
      }
      if (!userInfoList.contains(userInfo)) {
        userInfoList.addElement(userInfo);
      }
    }
  }

  private int getIndexOfUser(String username) {
    for (int i = 0; i < Global.tableUsers.size(); i++) {
      User user = (User) Global.tableUsers.elementAt(i);
      if (username.equals(user.getName())) {
        return i;
      }
    }
    return 0;
  }

  public void onRecieveData(DataPackage dataPackage) {
    int header = dataPackage.getHeader();
    switch (header) {
    case PREPARE_GAME_RESPONSE:
      userInfoList = new Vector();
      parseResponseToUserInfo(dataPackage.nextString());
      currentUserInfo = findUserInfoByUser(Global.currentUser);
      initChatComponent(userInfoList);
      break;
    case BET_RESPONSE:
      String str = dataPackage.nextString();
      if (str.equals("-1")) {
        GameGlobal.alert.showAlert(this, GameAlert.OK_BUTTON, "Số gold cược không hợp lệ");
      } else {
        parseResponseToUserInfo(str);
      }
      currentUserInfo = findUserInfoByUser(Global.currentUser);
      break;
    case COMMIT_RESPONSE:
      String strReceive = dataPackage.nextString();
      parseResponseToUserInfo(strReceive);
      // Kiem tra da co nguoi thang cuoc chua?
      isFinish();
      currentUserInfo = findUserInfoByUser(Global.currentUser);
      break;
    case USER_LEAVE_TABLE_RESPONSE:
      int status = dataPackage.nextInt();
      parseResponseToUserInfo(dataPackage.nextString());
      isFinish();
      String username = dataPackage.nextString();
      Global.currentTable.removeViewUser(username);
      removeUserInfo(username);
      if (status == -1) {
        GameGlobal.alert.showAlert2(this, GameAlert.OK_TYPE, "Chương đã dừng chơi", -1);
      }
      if (currentUserInfo.user.equals(username)) {
        GameGlobal.nextState(FrmGroupListTableFreeUserChat.instance(), null);
      }
      break;
    case USER_VIEW_JOIN_HEADER:
      if (userInfoList == null) {
        userInfoList = new Vector();
      }
      parseResponseToUserInfo(dataPackage.nextString());
      currentUserInfo = findUserInfoByUser(Global.currentUser);
      initChatComponent(userInfoList);
      break;
    case USER_VIEW_IN_BETTING_TIME_RESPONSE:
      waitingBetCount = dataPackage.nextInt() * 10;
      joinChatRoom();
      break;
    }
  }

  private void joinChatRoom() {
    // Create room name
    StringBuffer roomName = new StringBuffer(Global.currentGameService.getId());
    roomName.append("_");
    roomName.append(Global.currentGame.getId());
    roomName.append("_");
    roomName.append(Global.currentRoom.getId());
    roomName.append("_");
    roomName.append(Global.currentTable.getId());

    // Join chat room
    SocketClientUtil.createOrJoinChatRoom(roomName.toString(), "");

    // Init chat component
    if (Global.chatComponent != null) {
      Global.chatComponent.setCurrentRoomId(roomName.toString());
    }
  }

  private void initChatComponent(Vector userInfoList1) {
    // Init for chat
    TwoDimensionInt chatLocation = new TwoDimensionInt(userInfoList1.size(), 2);
    for (int i = 0; i < userInfoList1.size(); i++) {
      UserInfo userInfo = (UserInfo) userInfoList1.elementAt(i);
      int index = getIndexOfUser(userInfo.user);
      if (userInfo.position.equals("LeftCenter")) {
        chatLocation.set(index, 0, 29);
        chatLocation.set(index, 1, (GameConstants.SCREEN_HEIGHT / 2 - 72 / 2) + 20);
      } else if (userInfo.position.equals("RightCenter")) {
        chatLocation.set(index, 0, GameConstants.SCREEN_WIDTH - 70);
        chatLocation.set(index, 1, (GameConstants.SCREEN_HEIGHT / 2 - 72 / 2) + 20);
      } else if (userInfo.position.equals("TopLeft")) {
        chatLocation.set(index, 0, 10);
        chatLocation.set(index, 1, 72 / 2);
      } else if (userInfo.position.equals("TopRight")) {
        chatLocation.set(index, 0, GameConstants.SCREEN_WIDTH - 70);
        chatLocation.set(index, 1, 72 / 2);
      } else if (userInfo.position.equals("BottomLeft")) {
        chatLocation.set(index, 0, 14);
        chatLocation.set(index, 1, ((GameConstants.SCREEN_HEIGHT - DrawUtils.getBottomBarHeight()) - 72 / 2) - 20);
      } else if (userInfo.position.equals("BottomRight")) {
        chatLocation.set(index, 0, GameConstants.SCREEN_WIDTH - 70);
        chatLocation.set(index, 1, ((GameConstants.SCREEN_HEIGHT - DrawUtils.getBottomBarHeight()) - 72 / 2) - 20);
      }
    }
    Global.initChatComponent(chatLocation, chatLocation);
  }

  private void removeUserInfo(String username) {
    if (userInfoList != null && !userInfoList.isEmpty()) {
      for (int i = 0; i < userInfoList.size(); i++) {
        UserInfo userInfo = (UserInfo) userInfoList.elementAt(i);
        if (userInfo.user.equals(username)) {
          userInfoList.removeElementAt(i);
        }
      }
    }
  }

  private GameImage getCardImage(String cardName) {
    GameImage cardImage = (GameImage) cardImageList.get(cardName);
    if (cardImage == null) {
      cardImage = ImageUtil.getImage(Constants.CARD_IMAGE_CATEGORY, cardName + ".png");
      cardImageList.put(cardName, cardImage);
    }
    return cardImage;
  }

  public void destroy() {
    tableImage = null;
    userInfoList.removeAllElements();
    imgBaiUp = null;
    text8 = null;
    text13 = null;
    dollarImage = null;
    notifyImage = null;
    notifyEndGameImage = null;
    notifyImage2 = null;
    notifyEndGameImage2 = null;
    userInfoList = null;
    currentUserInfo = null;
    isFinish = false;
    isBet = false;
    cardImageList = null;
    defaultAvatar = null;
    clockImage = null;
    isShowBetBoard = false;
    myBetMoney = 0;
    txtBet = null;
    removeTimerListener();
  }

  private UserInfo findUserInfoByUser(User user) {
    if (userInfoList == null) {
      return null;
    }

    for (int i = 0; i < userInfoList.size(); i++) {
      UserInfo userInfo = (UserInfo) userInfoList.elementAt(i);
      if (userInfo.user.equals(user.getName())) {
        return userInfo;
      }
    }
    return null;
  }

  private boolean canCommit(UserInfo userInfo) {
    return userInfo != null && userInfo.status == 1;
  }

  private boolean isFinish() {
    if (userInfoList == null) {
      return false;
    }

    for (int i = 0; i < userInfoList.size(); i++) {
      UserInfo userInfo = (UserInfo) userInfoList.elementAt(i);
      if (userInfo.win == 1) {
        isFinish = true;
      }
    }
    return isFinish;
  }

  private String buildStringRequest(UserInfo userInfo) {
    // usr0,RightCenter,cards[1_Ro@true],status,point,win,master&usr1,LeftCenter,cards[2_Nhep@false],status,point,win,master
    StringBuffer sb = new StringBuffer();
    sb.append(userInfo.user);
    sb.append(",");
    sb.append(userInfo.position);
    sb.append(",");
    for (int j = 0; j < userInfo.card.len(); j++) {
      sb.append(userInfo.card.array[j].cardName);
      sb.append("@");
      sb.append(userInfo.card.array[j].isOpen);
      sb.append(";");
    }
    if (sb.toString().endsWith(";")) {
      sb.deleteCharAt(sb.length() - 1);
    }
    sb.append(",");
    sb.append(userInfo.status);
    sb.append(",");
    sb.append(userInfo.point);
    sb.append(",");
    sb.append(userInfo.win);
    sb.append(",");
    sb.append(userInfo.master);
    sb.append(",");
    sb.append(userInfo.bet);
    sb.append("&");
    return sb.toString();
  }

  /*
   * Submit len server de ha bai.
   */
  private void commit(UserInfo userInfo) {
    DataPackage dataPackage = Game.createGamePackage(COMMIT_REQUEST);
    StringBuffer sb = new StringBuffer();
    sb.append(buildStringRequest(userInfo));
    if (sb.toString().endsWith("&")) {
      sb.deleteCharAt(sb.length() - 1);
    }
    dataPackage.putString(sb.toString());
    Global.gameActionClient.write(dataPackage);
  }

  public void keyReleased(int keyCode) {
    if (currentUserInfo == null) {
      currentUserInfo = findUserInfoByUser(Global.currentUser);
    }
    if (!isShowBetBoard) {
      if (!Key.isCommandKey) {
        if (Global.chatComponent != null) {
          Global.chatComponent.setShowInputChatMessageTextField(true);
          Global.chatComponent.keyReleased(keyCode);
        }
        return;
      }
    }

    switch (keyCode) {
    case Key.FIRE:
      if (Global.isShowedInputChatBox()) {
        SocketClientUtil.sendMessage(Global.chatComponent.getCurrentRoomId(), Global.chatComponent.getChatTextField().getText());
        Global.hideInputChatBox();
        return;
      }
      if (currentUserInfo.status == VIEW_USER_STATUS) {
        return;
      }
      if (!isBet) {
        if (currentUserInfo.master == 0) {
          if (!isShowBetBoard) {
            isShowBetBoard = true;
            if (!txtBet.isVisible()) {
              txtBet.setVisible(true);
            }
            txtBet.setFocused(true);
          } else {
            myBetMoney = NumberUtil.parseInt(txtBet.getText());
            if (myBetMoney == NumberUtil.ERROR_VALUE) {
              Global.alert.showAlert(this, GameAlert.OK_TYPE, "Số gold cược phải là số");
              return;
            }
              
            if (myBetMoney > Global.currentUser.getGold()) {
              myBetMoney = Global.currentUser.getGold();
            }
            if (myBetMoney > Global.currentTable.getBid() * 5) {
              myBetMoney = Global.currentTable.getBid() * 5;
            }
            currentUserInfo.bet = myBetMoney;
            DataPackage dataPackage = Game.createGamePackage(BET_REQUEST);
            dataPackage.putInt(myBetMoney);
            Global.gameActionClient.write(dataPackage);
            isShowBetBoard = false;
            txtBet.setVisible(false);
          }
        }
      } else if (!isFinish) {
        if (canCommit(currentUserInfo)) {
          currentUserInfo.status = 2;
          commit(currentUserInfo);
        } else {
          if (currentUserInfo.status == 2) {
            return;
          }
          for (int i = 0; i < currentUserInfo.card.len(); i++) {
            if (!currentUserInfo.card.array[i].isOpen) {
              currentUserInfo.card.array[i].isOpen = true;
              break;
            }
          }
          int openCount = 0;
          for (int i = 0; i < currentUserInfo.card.len(); i++) {
            if (currentUserInfo.card.array[i].isOpen) {
              openCount++;
            }
          }
          if (openCount == currentUserInfo.card.len()) {
            currentUserInfo.status = 1;
          }
        }
      }
      break;
    case Key.SOFT_RIGHT:
      if (!isShowBetBoard) {
        if (Global.isShowedInputChatBox()) {
          Global.chatComponent.getChatTextField().deleteLastChar();
        }
      }
      if (isShowBetBoard) {
        if (txtBet.isVisible() && txtBet.isFocused()) {
          txtBet.deleteLastChar();
        }
      }
      if (!isFinish) {
        for (int i = 0; i < currentUserInfo.card.len(); i++) {
          if (!currentUserInfo.card.array[i].isOpen) {
            currentUserInfo.card.array[i].isOpen = true;
          }
        }
        currentUserInfo.status = 1;
      } else {
        // Truong hop ket thuc van. Tiep tuc choi
        GameGlobal.nextState(FrmGroupTableFreeUserFriend.instance(), null);
      }
      break;
    case Key.SOFT_LEFT:
      Global.hideInputChatBox();
      if (userInfoList.size() > 1) {
        if (currentUserInfo.status != VIEW_USER_STATUS) {
          GameGlobal.alert.showAlert2(this, GameAlert.YES_NO_TYPE, "Bạn muốn ngừng chơi?", 97);
        } else {
          SocketClientUtil.leaveTableRequest();
          Global.nextState(FrmGroupListTableFreeUserChat.instance(), null);
        }
      } else {
        Global.nextState(FrmGroupListTableFreeUserChat.instance(), null);
      }
      break;
    default:
      if (isShowBetBoard) {
        txtBet.keyReleased1(keyCode);
      }
      break;
    }
  }

  public void doTask() {
    if (isFinish()) {
      if (waitingContinueGame > 0) {
        waitingContinueGame--;
      }
      if (waitingContinueGame <= 0) {
        Global.nextState(FrmGroupTableFreeUserFriend.instance(), null);
      }
      return;
    }
    if (currentUserInfo == null) {
      return;
    }
    if (waitingBetCount > 0) {
      waitingBetCount--;
    }
    if (waitingBetCount <= 0) {
      isBet = true;
      isShowBetBoard = false;
      txtBet.setVisible(false);
    }
    if (isBet) {
      if (waitingCommitCount > 0) {
        waitingCommitCount--;
      }
      if (waitingCommitCount <= 0) {
        if (currentUserInfo.status != VIEW_USER_STATUS) {
          for (int i = 0; i < currentUserInfo.card.len(); i++) {
            if (!currentUserInfo.card.array[i].isOpen) {
              currentUserInfo.card.array[i].isOpen = true;
            }
            if (currentUserInfo.status != 2) {
              currentUserInfo.status = 2;
              commit(currentUserInfo);
            }
          }
        }
      }
    }
  }
  
  class CardArray {
    Card[] array;
    int len;
    
    void arraycopy(CardArray src, int srcPos, CardArray dest, int destPos, int length1) {
      System.arraycopy(src.array, srcPos, dest.array, destPos, len);
    }
    
    CardArray(Card[] value1, int length1) {
      array = value1;
      len = length1;
    }
    
    int len() {
      return len;
    }
    
    Card get(int i) {
      return array[i];
    }
    
    void set(int i, Card obj) {
      array[i] = obj;
    }
    
    void setArray(Card[] value1, int length1) {
      array = value1;
      len = length1;
    }
  }

  private class UserInfo {
    public String user;
    public String position;
    public CardArray card;
    public int status = 0; // 0: dang choi; 1: lat het; 2: ha bai; 3: choi tiep; 4: view user
    public String point = "0";
    public int win;
    public int master; // 1: chương
    public int showNotifyLatBaiCount = 0;
    public int showNotifyHaBaiCount = 0;
    public int bet = 0;

    public String toString() {
      return user + "|" + position;
    }
  }

  private class Card {
    public String cardName;
    public boolean isOpen;

    public String toString() {
      return cardName + "|" + isOpen;
    }
  }
}
